<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<script type="text/javascript">
	var setting = {
		check: {
			enable: true
		},
		data : {
			simpleData : {
				enable : true,
				idKey: "id",
				pIdKey: "parentId"
			}
		}
	};
	var zNodes = ${permissions};
	var zTree;
	$(document).ready(function() {
		zTree = $.fn.zTree.init($("#pztree"), setting, zNodes);
		//zTree.expandAll(true);
		zTree.setting.check.chkboxType = { "Y" : "ps", "N" : "s" };
		 $.ajax({
				type : "POST",
				data : {
					"resFormMap.userId" : "${param.userId}",
					"resFormMap.roleId" : "${param.roleId}"
				},
				url : '${pageContext.request.contextPath}/resources/findRes.shtml',
				dataType : 'json',
				success : function(json) {
					for (index in json) {
						if(json[index].id!=undefined)
					    zTree.checkNode( zTree.getNodeByParam( "id",json[index].id ), true ); 
					};
				}
			}); 
		 $("#subsucess").bind("click",
           function() {
			 var sNodes = zTree.getCheckedNodes(true);
				if (sNodes == undefined) {
					layer.alert("请选择对象!", {
						icon : 3
					});
				} else if (sNodes.length == 0) {
					layer.alert("请选择对象!", {
						icon : 3
					});
				} else {
					var resId = "";
					for(var i=0;i<sNodes.length;i++){
						resId += sNodes[i].id+",";
					};
					ly.ajax({
						async : false, //请勿改成异步，下面有些程序依赖此请数据
						type : "POST",
						data : {
							"userId" : "${param.userId}",
							"roleId" : "${param.roleId}",
							"resId":resId
						},
						url : '${pageContext.request.contextPath}/resources/addUserRes.shtml',
						dataType : 'json',
						success : function(json) {
							if (json == "success") {
								layer.alert('分配成功！', 3);
							} else {
								layer.alert(json, {
									icon : 2
								});
							}
							;
						}
					});
				}
			});
	});
</script>
<ul id="pztree" class="ztree"></ul>
<%@include file="/common/buttom.jspf"%>